package com.yulongtian.offers.day202209.day20220922;

/**
 * https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/?plan=lcof&plan_progress=g8d07hi
 *
 * @author YulongTian
 * @date 2022/9/22 - 16:31
 */
public class Test04 {
    public static void main(String[] args) {

    }

    public ListNode getKthFromEnd(ListNode head, int k) {
        int len=0;
        ListNode point=head;

        while (point!=null){
            len++;
            point=point.next;
        }
        if(len<k){
            return null;
        }
        point=head;
        int index=0;
        while (point!=null){
            index++;
            if(index+k-1==len){
                return point;
            }
            point=point.next;
        }

        return null;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

}
